package com.iexbuy.ihg.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.ui.fragment.BaseFragment;
import com.umeng.analytics.MobclickAgent;

public class SingleFragmentActivity extends BaseActivity {
	
	private static boolean isMainTab = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		App.sActivityList.add(this);
	}

	@Override
	public void init() {
//		initSlideRightBack();

	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		App.sActivityList.remove(this);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected BaseFragment getFragment() {
		Intent intent=getIntent();
		Class<? extends BaseFragment> cls = (Class<? extends BaseFragment>) intent.getSerializableExtra(ExtraKeyConstant.KEY_FRAGMENT);
		boolean isFinishAcitivity=intent.getBooleanExtra(ExtraKeyConstant.KEY_FINISH_ACTIVITY, true);
		setFinishActivity(isFinishAcitivity);
		if (cls == null)
			return null;
		
		BaseFragment fragment = null;
		try {
			fragment = cls.newInstance();
			Bundle bundle = getIntent().getBundleExtra(ExtraKeyConstant.KEY_BUNDLE);
			if (bundle != null) {
				fragment.setArguments(bundle);
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}

		return fragment;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(mCurrentFragment!=null){
			if(mCurrentFragment.onKeyDown(keyCode, event)){
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(0, R.anim.base_slide_right_out);
	}
}
